<template>
  <KView class="add-address">
    <page-header title="新增收货地址" @back-click="toBack">
      <KView class="add-address-btn" @click="toSave">保存</KView>
    </page-header>
    <KView class="add-address-line"></KView>

    <KView class="add-address-content">
      <address-form ref="form" @save-success="saveSuccess"></address-form>
    </KView>
  </KView>
</template>

<script>
  import PageHeader from 'common/pageHeader';
  import AddressForm from './addressForm';

  export default {
    data() {
      return {
        isMiniprogram: process.env.isMiniprogram
      };
    },

    methods: {
      toSave() {
        this.$refs['form'].saveAddress();
      },

      saveSuccess() {
        if (this.isMiniprogram) {
          let pages = getCurrentPages();
          let page = pages[pages.length - 2];
          page.window.actionData = {
            type: 'resetData',
            data: {
              type: 'add'
            }
          };
          wx.navigateBack();
        }
      },

      toBack() {
        if (!this.isMiniprogram) {
          window.history.back();
          return;
        }
        wx.navigateBack();
      }
    },

    components: {
      PageHeader,
      AddressForm
    }
  };

</script>

<style lang="scss">
  .add-address {
    height: 100%;

    .page-header {
      margin-bottom: formatPx(20);

      .add-address-btn {
        position: absolute;
        right: formatPx(30);
        bottom: 0;
        font-size: formatPx(26);
        color: #59c264;
      }
    }

    .add-address-line {
      height: 1px;
      background: #DADADA;
    }

    .add-address-content {
      height: calc(100% - #{formatPx(95)});
      padding: 0 formatPx(30);
      overflow: auto;
    }
  }

</style>
